layui.use(['table', 'jquery'], function () {
    const table = layui.table,
        $ = layui.jquery;

    //显示日期
    showTime('#startDate');
    showTime('#endDate');

    //默认加载时执行
    findAll();
    findCodeByType('gender');

    function findAll() {

        let code = $('#code').val();
        let username = $('#username').val();
        let gender = $("#gender option:selected").val();
        let startDate = $('#startDate').val();
        let endDate = $('#endDate').val();
        let params = {
            code: code,
            username: username,
            gender: gender,
        };
        if (startDate !== undefined && startDate != null && startDate !== '') {
            params['startDate'] = startDate
        }
        if (endDate !== undefined && endDate != null && endDate !== '') {
            params['endDate'] = endDate
        }

        let cols = [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 80, title: 'ID', sort: true, fixed: 'left'}
            , {field: 'code', width: 120, title: '用户编号', sort: true}
            , {field: 'username', width: '15%', title: '用户名'}
            , {field: 'nickname', width: '15%', title: '昵称'}
            , {
                field: 'gender', width: 80, title: '性别', templet: e => {
                    return getGenderStatus(e.gender);
                }
            }
            , {field: 'phone', width: '15%', title: '电话'}
            , {field: 'createTime', title: '创建时间', width: '15%'}
            , {field: 'updateTime', title: '更新时间', width: '15%'}
            , {fixed: 'right', width: 100, align: 'center', toolbar: '#operating'}
        ]];

        //发送请求
        table.render({
            elem: '#dataList'
            , url: LOCAL_CROSS_URL + "/user/findAll"
            , where: params
            , method: 'get'
            , headers: HEADERS
            , request: REQUEST
            , response: RESPONSE
            , cellMinWidth: 80
            , height: 720
            , skin: 'line'
            , even: true
            , page: true
            , limits: [5, 10, 20, 30, 50]
            , limit: 10
            , title: '用户数据表'
            , cols: cols
        });
    }

    //监听行工具事件
    table.on('tool(dataList)', function (obj) {
        const data = obj.data //获得当前行数据
            , layEvent = obj.event;
        if (layEvent === 'edit') {
            distributeRole(data.code);
        }
    });


    /**
     * 分配角色
     * @param code
     */
    function distributeRole(code) {
        sessionStorage.setItem("pmsUserCode", code);
        layer.open({
            title: '分配角色',
            area: ["400px", "350px"],
            shadeClose: true,
            anim: 2,
            type: 2,
            content: 'pmsDistributeRole.html'
        });
    }

    /**
     * 查询
     */
    $('#query').on('click', function () {
        findAll()
    });

    /**
     * 重置
     */
    $('#reset').on('click', function () {
        $('#code').val('');
        $('#username').val('');
        $('#gender').val('');
        $('#startDate').val('');
        $('#endDate').val('');
    });

    /**
     * 查询码表
     */
    function findCodeByType(type) {
        let data = {
            type: type
        };
        let result = commonAjax("/code/findByType", data, "get");
        let html = '<option value="">全部</option>';
        for (let i = 0; i < result.data.length; i++) {
            html += '<option value="' + result.data[i].code + '">' + result.data[i].value + '</option>';
        }
        $("#gender").html(html);
    }

});